package com.hzzh.servlet;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hzzh.dao.DepartmentDAO;
import com.hzzh.dao.MeetingRoomDAO;
import com.hzzh.service.MeetingService;
import com.hzzh.vo.Department;
import com.hzzh.vo.Meeting;
import com.hzzh.vo.MeetingRoom;

/**
 * Servlet implementation class BookMeetingServlet
 */
@WebServlet("/BookMeetingServlet")
public class BookMeetingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookMeetingServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		// 查询会议室和部门 之后跳转到bookmeeting.jsp
		String code = request.getParameter("code");
		if (code != null && code.equals("prepare")) {
			MeetingRoomDAO roomDao = new MeetingRoomDAO();
			DepartmentDAO deptDao = new DepartmentDAO();
			List<MeetingRoom> roomsList = roomDao.selectAllMeetingRooms();
			List<Department> deptsList = deptDao.selectAllDepartmentDAO();

			request.setAttribute("roomsList", roomsList);
			request.setAttribute("deptsList", deptsList);

			request.getRequestDispatcher("bookmeeting.jsp").forward(request, response);
		}

		if (code != null && code.equals("book")) {
			MeetingService meetingService = new MeetingService();

			String meetingname = request.getParameter("meetingname");
			int roomid = Integer.parseInt(request.getParameter("roomid"));

			HttpSession session = request.getSession();
			int reservationistid = (Integer) session.getAttribute("employeeid");

			int numofparticipants = Integer.parseInt(request.getParameter("numofparticipants"));
			Timestamp starttime = Timestamp.valueOf(request.getParameter("starttime"));
			Timestamp endtime = Timestamp.valueOf(request.getParameter("endtime"));
			Timestamp reservationtime = new Timestamp(System.currentTimeMillis());
			Timestamp canceledtime = null;
			String status = "0";
			String description = request.getParameter("description");

			Meeting meeting = new Meeting(meetingname, roomid, reservationistid, numofparticipants, starttime, endtime,
					reservationtime, canceledtime, description, status);

			String[] employeesid = request.getParameterValues("selSelectedEmployees");

			List<Integer> employeesidList = new ArrayList<Integer>();

			for (String s : employeesid) {
				employeesidList.add(Integer.parseInt(s));
			}

			meetingService.bookMeeting(meeting, employeesidList);
			request.getRequestDispatcher("ViewMyBookingServlet").forward(request, response);

		}

	}

}
